//
//  CategoryController.m
//  i优购商城
//
//  Created by qianfeng on 15/9/14.
//  Copyright (c) 2015年 qianfeng. All rights reserved.
//

#import "CategoryController.h"
#import "HttpRequestManager.h"
#import "CategoryCell.h"
#import "CollectionViewCell.h"
#import "UIImageView+AFNetworking.h"
@interface CategoryController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
{
    NSMutableArray*_dataArry;
}
@property (weak, nonatomic) IBOutlet UICollectionView *myCollectionView;

@end

@implementation CategoryController

- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        _dataArry=[[NSMutableArray alloc]init];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [_myCollectionView registerNib:[UINib nibWithNibName:@"CollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"CollectionViewCell"];
    [self loadDataFromNet];
}

-(void)loadDataFromNet
{
    [[HttpRequestManager shareInstance]getCategoryDataSuccess:^(id responseobject) {
        NSDictionary*dic=responseobject[@"categories_pictext"];
        [_dataArry addObjectsFromArray:dic[@"hot_categories"]];
        [_dataArry addObjectsFromArray:dic[@"advert"]];
        [_dataArry addObjectsFromArray:dic[@"hot_recommend"][@"recommends"]];
        [_myCollectionView reloadData];
    } Failure:^(NSError *error) {
        NSLog(@"请求数据失败error===%@",error);
    }];


}


-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
  
    return _dataArry.count;

}

-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary*dic=_dataArry[indexPath.row];
    if (indexPath.row==9) {
        CollectionViewCell*cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"CollectionViewCell" forIndexPath:indexPath];
        [cell.imgView setImageWithURL:[NSURL URLWithString:dic[@"adImg"]]];
        cell.backgroundColor=[[UIColor lightGrayColor]colorWithAlphaComponent:0.2];
        return cell;
    }
    else
    {
    CategoryCell*cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"CategoryCell" forIndexPath:indexPath];
    [cell.imgView setImageWithURL:[NSURL URLWithString:dic[@"categoryImg"]]];
    cell.nameLbl.text=dic[@"categoryName"];
    return cell;
    }


}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row==9) {
        return CGSizeMake(self.view.bounds.size.width,self.view.bounds.size.width*100/320);
    }
    else
   return CGSizeMake((self.view.bounds.size.width-2)/3 ,(self.view.bounds.size.width-2)/3*130/106);
}


@end
